#pragma once

#include <QObject>
#include <QHash>
#include <QList>
#include <QVector>
#include <memory>
#include "base_lift.h"
#include "kernel/interfaces/ILogger.h"

namespace application {
namespace lift {

/**
 * @brief 电梯管理器
 * @details 管理所有电梯的请求和调度
 */
class LiftManager : public QObject {
    Q_OBJECT

public:
    explicit LiftManager(std::shared_ptr<kernel::ILogger> logger = nullptr, QObject* parent = nullptr);
    ~LiftManager() = default;

    /**
     * @brief 添加电梯
     */
    void addLift(std::shared_ptr<BaseLift> lift);

    /**
     * @brief 移除电梯
     */
    void removeLift(int liftNum);

    /**
     * @brief 获取电梯
     */
    std::shared_ptr<BaseLift> getLift(int liftNum) const;

    /**
     * @brief 请求电梯
     * @param liftNum 电梯编号
     * @return 电梯指针，失败返回nullptr
     */
    std::shared_ptr<BaseLift> requestLift(int liftNum);

    /**
     * @brief 释放电梯
     */
    bool releaseLift(int liftNum);

    /**
     * @brief 获取楼层信息
     */
    FloorInfo getFloorInfo(int liftNum, int floor) const;

    /**
     * @brief 获取电梯请求信息
     * @param startFloor 起始楼层
     * @param targetFloor 目标楼层
     */
    LiftRequestInfo getLiftRequestInfo(int startFloor, int targetFloor) const;

    /**
     * @brief 查找跨楼层路径，可能包含多部电梯
     */
    QVector<LiftSegment> findLiftPath(int startFloor, int targetFloor) const;

    /**
     * @brief 获取所有电梯
     */
    QList<std::shared_ptr<BaseLift>> getAllLifts() const;

    /**
     * @brief 获取空闲电梯
     */
    std::shared_ptr<BaseLift> getIdleLift() const;

    /**
     * @brief 初始化所有电梯
     */
    void initializeAllLifts();

signals:
    void liftAdded(int liftNum);
    void liftRemoved(int liftNum);
    void liftRequestCompleted(int liftNum);
    void liftRequestFailed(int liftNum, const QString& error);

private:
    QHash<int, std::shared_ptr<BaseLift>> m_lifts;
    std::shared_ptr<kernel::ILogger> m_logger;
};

} // namespace lift
} // namespace application
